package cn.tedu.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

//模拟主题模式的生产者
public class TopicProducer {
    public static void main(String[] args) throws Exception {
        //1.创建连接和channel
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("127.0.0.1");
        cf.setUsername("guest");
        cf.setPassword("guest");
        Connection nc = cf.newConnection();
        Channel cc = nc.createChannel();
        //第一个参数是交换机名称；第二个参数是交换机类型
        cc.exchangeDeclare("type_topic", BuiltinExchangeType.TOPIC);
        //在控制台循环发送数据
        while (true){
            Scanner scanner = new Scanner(System.in);
            System.out.print("输入消息数据:");
            String msg = scanner.nextLine();
            System.out.print("路由键:");
            String routingKey = scanner.nextLine();
            if (msg.equals("quit"))
                break;
            //第一个参数是交换机名称
            //第二个参数指定携带的路由键
            cc.basicPublish("type_topic",routingKey,null,msg.getBytes());
        }
        nc.close();
    }
}
